package com.funong.matrix.biz.massage.domain;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.funong.matrix.biz.base.dao.SysSendTaskDao;
import com.funong.matrix.biz.base.entity.SysMsgBusinessTemplateEntity;
import com.funong.matrix.biz.base.entity.SysSendTaskEntity;
import com.funong.matrix.biz.massage.pojo.SendMessageDTO;
import com.funong.matrix.core.dto.QueryDTO;
import com.funong.matrix.core.dto.Result;
import com.funong.matrix.core.enums.MessageSendStatusEnum;
import com.funong.matrix.core.utils.DateTimeUtil;
import com.funong.matrix.core.utils.JwtUtils;
import com.funong.matrix.core.utils.StringUtils;
import com.github.pagehelper.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

/**
 * @author : NaNa
 * @since : 2023-05-19 16:56
 **/
@Slf4j
@Service
public class SysSendTaskDomainDao {

    @Resource
    private SysSendTaskDao sendTaskDao;

    public boolean save(SysSendTaskEntity entity){
        String currentUserName = JwtUtils.getCurrentUserName();
        if(StringUtils.isNotBlank(currentUserName)){
            entity.setCreatedBy(currentUserName);
        }
        entity.setCreatedAt(DateTimeUtil.now())
                .setUpdatedAt(DateTimeUtil.now());
        return sendTaskDao.save(entity);
    }

    public boolean updateById(SysSendTaskEntity entity) {
        String currentUserName = JwtUtils.getCurrentUserName();
        if(StringUtils.isNotBlank(currentUserName)){
            entity.setUpdatedBy(currentUserName);
        }
        entity.setUpdatedAt(DateTimeUtil.now());
        entity.setUpdatedAt(Timestamp.valueOf(LocalDateTime.now()));
        return sendTaskDao.updateById(entity);
    }

    /**
     * 记录task信息
     *
     * @param sendMessageDTO
     * @param businessTemplateDTO
     * @return
     */
    public SysSendTaskEntity recordTask(SendMessageDTO sendMessageDTO, SysMsgBusinessTemplateEntity businessTemplateDTO) {
        SysSendTaskEntity sendTaskEntity = new SysSendTaskEntity();


        sendTaskEntity.setTitle(sendMessageDTO.getTitle())
                .setContent(sendMessageDTO.getContent())
                .setReceiver(businessTemplateDTO.getReceiver())
                .setReceiverType(businessTemplateDTO.getReceiverType())
                .setBusinessTemplateId(businessTemplateDTO.getTemplateId())
                .setSender(businessTemplateDTO.getSender())
                .setCopyer(businessTemplateDTO.getCopyer())
                .setCopyerType(businessTemplateDTO.getCopyerType())
                .setMessageType(sendMessageDTO.getMessageType())
                .setReferId(sendMessageDTO.getReferBizId())
                .setBusinessCode(sendMessageDTO.getBusinessCode())
                .setStatus(MessageSendStatusEnum.UNSEND.name())
                .setCreatedAt(Timestamp.valueOf(LocalDateTime.now()))
        ;
        sendTaskDao.save(sendTaskEntity);
        return sendTaskEntity;
    }

    /**
     * 发送列表
     *
     * @param queryDTO
     * @return
     */
    public Result queryUnSendTask(QueryDTO<SysSendTaskEntity> queryDTO,int subMinute) {
        Page<SysSendTaskEntity> page = new Page<>(queryDTO.getPageNo(), queryDTO.getPageSize());
        SysSendTaskEntity condition = queryDTO.getCondition();
        IPage<SysSendTaskEntity> iPage = sendTaskDao.getBaseMapper().selectPage(page, Wrappers.<SysSendTaskEntity>lambdaQuery()
                .eq(StringUtil.isNotEmpty(condition.getBusinessCode()), SysSendTaskEntity::getBusinessCode, condition.getBusinessCode())
                .in(StringUtil.isNotEmpty(condition.getStatus()), SysSendTaskEntity::getStatus, condition.getStatus())
                .lt(SysSendTaskEntity::getCreatedAt, DateTimeUtil.addMinute(-subMinute))
                .orderByDesc(SysSendTaskEntity::getId));
        return Result.page(iPage, iPage.getRecords());
    }

    /**
     * 发送列表
     *
     * @param queryDTO
     * @return
     */
    public Result querySendTask(QueryDTO<SysSendTaskEntity> queryDTO) {
        Page<SysSendTaskEntity> page = new Page<>(queryDTO.getPageNo(), queryDTO.getPageSize());
        SysSendTaskEntity condition = queryDTO.getCondition();
        IPage<SysSendTaskEntity> iPage = sendTaskDao.getBaseMapper().selectPage(page, Wrappers.<SysSendTaskEntity>lambdaQuery()
                .eq(StringUtil.isNotEmpty(condition.getBusinessCode()), SysSendTaskEntity::getBusinessCode, condition.getBusinessCode())
                .eq(StringUtil.isNotEmpty(condition.getStatus()), SysSendTaskEntity::getStatus, condition.getStatus())
                .orderByDesc(SysSendTaskEntity::getId));
        List<SysSendTaskEntity> sendTaskEntityList = iPage.getRecords();
        return Result.page(iPage, sendTaskEntityList);
    }
}
